﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
    public class productDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();

        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int productId)
        {
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("select count(1) from product where productId=@productId and isCan=0");
            htSyn.Add("productId", productId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 是否存在产品型号---郑同飞
        /// <summary>
        /// 是否存在产品型号---郑同飞
        /// </summary>
        /// <param name="proModel"></param>
        /// <returns></returns>
        public bool Exists(string proModel)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select productModel from product where productModel=@proModel and isCan=0");
            htSyn.Add("proModel", proModel);
            if (db.GetDataTable(strSql.ToString(),htSyn).Rows.Count>0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 查询每个类别下面的产品根据类别号
        /// <summary>
        /// 查询每个类别下面的产品根据类别号
        /// </summary>
        public int countByClass(int pclassid)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(productid) from product where pClassId=@pclassid and isCan=0");
            htSyn.Add("@pclassid", pclassid);
            return  db.GetRecordCount(strSql.ToString(), htSyn);
        }
        #endregion
        
        #region 获得产品数据列表
        /// <summary>
        /// 获得产品数据列表
        /// </summary>
        /// <returns></returns>
        public IList<product> GetModel()
        {
            strSql.Remove(0, strSql.Length);
            strSql.Append("select productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark from product where  isCan=0");
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 获得产品数据表
        /// <summary>
        /// 获得产品数据表
        /// </summary>
        /// <returns></returns>
        public DataTable getDataTable()
        {

            strSql.Append("select productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark from product where  isCan=0 order by pSort");
            return db.GetDataTable(strSql.ToString());
        }
        #endregion

        #region 根据产品号查询实体
        /// <summary>
        /// 根据产品号查询实体
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public product GetModel(int productId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select  productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark from product where productId=@productId ");
            htSyn.Add("@productId", productId);
            DataTable dt = db.GetDataTable(strSql.ToString(),htSyn); 
            product product = new product();
            if (dt.Rows.Count>0)
            {
                product.productId = Convert.ToInt32(dt.Rows[0]["productId"]);
                product.productName = dt.Rows[0]["productName"].ToString();
                product.Price = Convert.ToDecimal(dt.Rows[0]["Price"]);
                product.pInfo = dt.Rows[0]["pInfo"].ToString();
                product.pClassId = Convert.ToInt32(dt.Rows[0]["pClassId"]);
                product.pTitle = dt.Rows[0]["pTitle"].ToString();
                product.pKey = dt.Rows[0]["pKey"].ToString();
                product.pDescription = dt.Rows[0]["pDescription"].ToString();
                product.pButtom = dt.Rows[0]["pButtom"].ToString();
                product.isCan = Convert.ToBoolean(dt.Rows[0]["isCan"]);
                product.isBast = Convert.ToBoolean(dt.Rows[0]["isBast"]);
                product.IsNew = Convert.ToBoolean(dt.Rows[0]["IsNew"]);
                product.isIndex = Convert.ToBoolean(dt.Rows[0]["isIndex"]);
                product.pDate = Convert.ToDateTime(dt.Rows[0]["pDate"]);
                product.eDate = Convert.ToDateTime(dt.Rows[0]["eDate"]);
                product.pSourse = dt.Rows[0]["pSourse"].ToString();
                product.ptag = dt.Rows[0]["ptag"].ToString();
                product.pSort = Convert.ToInt32(dt.Rows[0]["pSort"]);
                product.Views = Convert.ToInt32(dt.Rows[0]["Views"]);
                product.Productmodel = dt.Rows[0]["ProductModel"].ToString();
                product.Relationmark =Convert.ToInt32 (dt.Rows[0]["Relationmark"]);
            }     
            return product;
        }
        #endregion

        #region 修改产品
        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(product model)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update product set ");
            strSql.Append("productName=@productName,");
            strSql.Append("Price=@Price,");
            strSql.Append("pInfo=@pInfo,");
            strSql.Append("pClassId=@pClassId,");
            strSql.Append("pTitle=@pTitle,");
            strSql.Append("pKey=@pKey,");
            strSql.Append("pDescription=@pDescription,");
            strSql.Append("pButtom=@pButtom,");            
            strSql.Append("eDate=@eDate,");
            strSql.Append("pSourse=@pSourse,");
            strSql.Append("ptag=@ptag,");
            strSql.Append("pSort=@pSort,");
            strSql.Append("Views=@Views,");
            strSql.Append("productModel=@productmodel,");
            strSql.Append("Relationmark=@relationmark");
            strSql.Append(" where productId=@productId ");
            htSyn.Add("@productName", model.productName);
            htSyn.Add("@Price", model.Price);
            htSyn.Add("@pInfo", model.pInfo);
            htSyn.Add("@pClassId", model.pClassId);
            htSyn.Add("@pTitle", model.pTitle);
            htSyn.Add("@pKey", model.pKey);
            htSyn.Add("@pDescription", model.pDescription);
            htSyn.Add("@pButtom", model.pButtom);            
            htSyn.Add("@eDate", model.eDate);
            htSyn.Add("@pSourse", model.pSourse);
            htSyn.Add("@ptag", model.ptag);
            htSyn.Add("@pSort", model.pSort);
            htSyn.Add("@Views", model.Views);
            htSyn.Add("@productmodel",model.Productmodel); 
            htSyn.Add("@productId", model.productId);
            htSyn.Add("@relationmark", model.Relationmark);
            if (db.ExecuteNonQuery(strSql.ToString(),htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 快速修改产品
        /// <summary>
        /// 快速修改产品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool updateFast(product model)
        {
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("update product set ");
            strSql.Append("isCan=@isCan,");
            strSql.Append("isBast=@isBast,");
            strSql.Append("IsNew=@IsNew,");
            strSql.Append("isIndex=@isIndex,");
            strSql.Append("eDate=@eDate,");
            //strSql.Append("pSourse=@pSourse,");
            strSql.Append("pSort=@pSort,");
            strSql.Append("Views=@Views,");
            strSql.Append("Relationmark=@relationmark");
            strSql.Append(" where productId=@productId ");
            htSyn.Add("@isCan", model.isCan);
            htSyn.Add("@isBast", model.isBast);
            htSyn.Add("@IsNew", model.IsNew);
            htSyn.Add("@isIndex", model.isIndex);
            htSyn.Add("@eDate", model.eDate);
            //htSyn.Add("@pSourse", model.pSourse);
            htSyn.Add("@pSort", model.pSort);
            htSyn.Add("@Views", model.Views);
            htSyn.Add("@productId", model.productId);
            htSyn.Add("@relationmark",model.Relationmark);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 快速推荐
        public bool isBast(bool bo,int id)
        {
            htSyn.Clear();
            string strSql = "update product set isBast=@bo where productId=@id";
            htSyn.Add("bo", !bo);
            htSyn.Add("id", id);
            if (db.ExecuteNonQuery(strSql, htSyn) > 0)
                return !bo;
            else
                return bo;
        }
        #endregion

        #region 快速屏蔽
        public bool isCan(bool bo, int id)
        {

            htSyn.Clear();
            string strSql = "update product set isCan=@bo where productId=@id";
            htSyn.Add("bo", !bo);
            htSyn.Add("id", id);
            if (db.ExecuteNonQuery(strSql, htSyn) > 0)
                return !bo;
            else
                return bo;
        }
        #endregion

        #region 最新设置
        public bool IsNew(bool bo, int id)
        {
            htSyn.Clear();
            string strSql = "update product set IsNew=@bo where productId=@id";
            htSyn.Add("bo", !bo);
            htSyn.Add("id", id);
            if (db.ExecuteNonQuery(strSql, htSyn) > 0)
                return !bo;
            else
                return bo;
        }
        #endregion

        #region 首页显示
        public bool isIndex(bool bo, int id)
        {

            htSyn.Clear();
            string strSql = "update product set isIndex=@bo where productId=@id and isCan=0";
            htSyn.Add("bo", !bo);
            htSyn.Add("id", id);
            if (db.ExecuteNonQuery(strSql, htSyn) > 0)
                return !bo;
            else
                return bo;
        }
        #endregion

        #region 删除产品根据产品号
        /// <summary>
        /// 删除产品根据产品号
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public bool Delete(int productId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from product where productId=@productId");
            htSyn.Add("productId", productId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 批量删除产品根据产品号
        /// <summary>
        /// 批量删除产品根据产品号
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public bool Delete(string[] pId)
        {
          
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from product where");
            htSyn.Clear();
            foreach (string id in pId)
            {
                strSql.AppendFormat(" productid={0} or", id);
            }
            strSql = strSql.Remove(strSql.Length - 1, 1);
            strSql = strSql.Remove(strSql.Length - 1, 1);
            if (db.ExecuteNonQuery(strSql.ToString()) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 批量移动产品类别根据产品父类别号
        /// <summary>
        /// 批量移动产品类别根据产品父类别号
        /// </summary>
        /// <param name="id">产品号</param>
        ///  <param name="classid">要移动到的类别号</param>
        /// <returns></returns>
        public bool Update(string[] id, string classid)
        {
            strSql.Remove(0, strSql.Length);
            strSql.Append("update product set");
            strSql.AppendFormat(" pclassid={0}", classid);
            strSql = strSql.Append(" where ");
            foreach (string pid in id)
            {
                strSql.AppendFormat(" productid={0} or", pid);
            }
            strSql = strSql.Remove(strSql.Length - 1, 1);
            strSql = strSql.Remove(strSql.Length - 1, 1);
            if (db.ExecuteNonQuery(strSql.ToString()) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据产品id集合获得数据---郑同飞
        /// <summary>
        /// 根据产品id集合获得数据---郑同飞
        /// </summary>
        /// <param name="productids"></param>
        /// <returns></returns>
        public IList<product> GetModel(string[] productids)
        {
            strSql.Remove(0,strSql.Length);
            strSql.Append("select productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark from product");
            strSql.Append(" where ");
            foreach (string pid in productids)
            {
                strSql.AppendFormat(" productid={0} or",pid);
            }
            strSql = strSql.Remove(strSql.Length - 1, 1);
            strSql = strSql.Remove(strSql.Length - 1, 1);
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 根据关联标记获得数据---郑同飞
        /// <summary>
        /// 根据关联标记获得数据---郑同飞
        /// </summary>
        /// <param name="mark"></param>
        /// <param name="productId"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public DataTable GetModelByMark(int mark,int productId,int count)
        {
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.AppendFormat("select top {0} productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark from product where Relationmark=@relationmark and ProductId!=@productid and Relationmark!=0", count);
            htSyn.Add("@relationmark",mark);           
            htSyn.Add("@productid",productId);
            return db.GetDataTable(strSql.ToString(), htSyn);
        }
        #endregion

        #region 添加产品
        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Insert(product model)
        {

            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into product([productName],[Price],[pInfo],[pClassId],[pTitle],[pKey],[pDescription],[pButtom],[isCan],[isBast],[IsNew],[isIndex],[pDate],[eDate],[pSourse],[ptag],[pSort],[Views],[ProductModel],Relationmark) values (@productName,@Price,@pInfo,@pClassId,@pTitle,@pKey,@pDescription,@pButtom,@isCan,@isBast,@IsNew,@isIndex,@pDate,@eDate,@pSourse,@ptag,@pSort,@Views,@productmodel,@relationmark)");
            htSyn.Clear();
            htSyn.Add("@productName", model.productName);
            htSyn.Add("@Price", model.Price);
            htSyn.Add("@pInfo", model.pInfo);
            htSyn.Add("@pClassId", model.pClassId);
            htSyn.Add("@pTitle", model.pTitle);
            htSyn.Add("@pKey", model.pKey);
            htSyn.Add("@pDescription", model.pDescription);
            htSyn.Add("@pButtom", model.pButtom);
            htSyn.Add("@isCan", model.isCan);
            htSyn.Add("@isBast", model.isBast);
            htSyn.Add("@IsNew", model.IsNew);
            htSyn.Add("@isIndex", model.isIndex);
            htSyn.Add("@pDate", model.pDate);
            htSyn.Add("@eDate", model.eDate);
            htSyn.Add("@pSourse", model.pSourse);
            htSyn.Add("@ptag", model.ptag);
            htSyn.Add("@pSort", model.pSort);
            htSyn.Add("@Views", model.Views);
            htSyn.Add("@productmodel",model.Productmodel);
            htSyn.Add("@relationmark",model.Relationmark);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<product> getProductsBySql(string strSql)
        {
            IList<product> list = new List<product>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    product product = new product();
                    product.productId = Convert.ToInt32(dr["productId"]);
                    product.productName = dr["productName"].ToString();
                    product.Price = Convert.ToDecimal(dr["Price"]);
                    product.pInfo = dr["pInfo"].ToString();
                    product.pClassId = Convert.ToInt32(dr["pClassId"]);
                    product.pTitle = dr["pTitle"].ToString();
                    product.pKey = dr["pKey"].ToString();
                    product.pDescription = dr["pDescription"].ToString();
                    product.pButtom = dr["pButtom"].ToString();
                    product.isCan = Convert.ToBoolean(dr["isCan"]);
                    product.isBast = Convert.ToBoolean(dr["isBast"]);
                    product.IsNew = Convert.ToBoolean(dr["IsNew"]);
                    product.isIndex = Convert.ToBoolean(dr["isIndex"]);
                    product.pDate = Convert.ToDateTime(dr["pDate"]);
                    product.eDate = Convert.ToDateTime(dr["eDate"]);
                    product.pSourse = dr["pSourse"].ToString();
                    product.ptag = dr["ptag"].ToString();
                    product.pSort = Convert.ToInt32(dr["pSort"]);
                    product.Views = Convert.ToInt32(dr["Views"]);
                    product.Productmodel = dr["productModel"].ToString();
                    product.Relationmark =Convert.ToInt32(dr["Relationmark"]);
                    list.Add(product);
                }
            }
            return list;
        }
        #endregion

        #region 后台产品分页，多次重载---郑同飞

        #region  后台产品分页--类别，关键字
        /// <summary>
        /// 后台产品分页--类别，关键字
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="classid">类别</param>
        /// <param name="key">搜索关键字</param>
        /// <returns></returns>
        public DataTable pagingForProduct(int pageIndex,int pageSize,int classid,string key)
        {

            return db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and pClassId='" + classid + "' and productName like '%" + key + "%' and iscan=0 ", "productid", false);
        }
        #endregion

        #region 后台产品分页--类别 
        /// <summary>
        /// 后台产品分页--类别 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="classid">类别</param>
        /// <returns></returns>
        public DataTable pagingForProduct(int pageIndex, int pageSize, int classid)
        {
            return db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and pClassId='" + classid + "' and iscan=0 ", "productid", false);
        }
        #endregion

        #region 后台产品分页--关键字
        /// <summary>
        /// 后台产品分页--关键字
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="key">搜索关键字</param>
        /// <returns></returns>
        public DataTable pagingForProduct(int pageIndex, int pageSize, string key) 
        {
            return db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and productName like '%" + key + "%' and iscan=0 ", "productid", false);
        }
        #endregion

        #region 后台产品分页
        /// <summary>
        /// 后台产品分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public DataTable pagingForProduct(int pageIndex, int pageSize)
        {
            return db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and iscan=0 ", "productid", false);
        }
        #endregion          

        #region 后台屏蔽产品分页---郑同飞
        /// <summary>
        /// 后台屏蔽产品分页---郑同飞
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="isCan">是否屏蔽（屏蔽：true,不屏蔽：false）</param>
        /// <returns></returns>
        public DataTable pagingForProduct(int pageIndex, int pageSize, bool isCan)
        {

            return db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and isCan =" + Convert.ToDouble(isCan), "productid", false);
        }
        #endregion
        #endregion

        #region 获的产品总数,多次重载---郑同飞

        #region 获的产品总数--类别
        /// <summary>
        /// 根据类别获的产品总数
        /// </summary>
        /// <param name="classid">类别</param>
        /// <returns></returns>
        public int getRecordCount(int classid)
        {
            object objRecordCount = null;
            int i = 0;
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("select count(1) from product where  pclassid=@classid");
            htSyn.Add("@classid", classid);
            objRecordCount = db.ExecuteScalar(strSql.ToString(), htSyn);
            if (objRecordCount != null)
            {
                i=Convert.ToInt32(objRecordCount);
            }
            return i;
        }
        #endregion

        #region 获的产品总数
        /// <summary>
        /// 获的产品总数  
        /// </summary>
        /// <returns></returns>
        public int getRecordCount() 
        {
            object objRecordCount = null;
            int i = 0;
            objRecordCount = db.ExecuteScalar("select count(1) from product");
            if (objRecordCount != null)
            {
                i = Convert.ToInt32(objRecordCount);
            }
            return i;

        }
        #endregion

        #region 获得产品总数--类别，关键字
        /// <summary>
        /// 获得产品总数--类别，关键字
        /// </summary>
        /// <param name="classid">类别</param>
        /// <param name="where">关键字</param>
        /// <returns></returns>
        public int getRecordCount(int classid,string where)
        {
           
            object objRecordCount = null;
            int i = 0;
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("select count(1) from product where iscan=0 and pClassId=@classid and productName like @where");
            htSyn.Add("@classid", classid);
            htSyn.Add("@where", "%" + where + "%");
            objRecordCount = db.ExecuteScalar(strSql.ToString(), htSyn);
            if (objRecordCount != null)
            {
                i = Convert.ToInt32(objRecordCount);
            }
            return i;
        } 
        #endregion

        #region 获得产品总数--关键字
        /// <summary>
        /// 获得产品总数--关键字
        /// </summary>
        /// <param name="where">关键字</param>
        /// <returns></returns>       
        public int getRecordCount(string where)
        {
           
            object objRecordCount = null;
            int i = 0;
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("select count(1) from product where iscan=0 and productName like @where");
            htSyn.Add("@where", "%" + where + "%");
            objRecordCount = db.ExecuteScalar(strSql.ToString(), htSyn);
            if (objRecordCount != null)
            {
                i = Convert.ToInt32(objRecordCount);
            }
            return i;
        } 
        #endregion

        #region 获得屏蔽产品总数
        public int getRecordCount(bool isCan) 
        {        
          
            object objRecordCount = null;
            int i = 0;
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("select count(1) from product where isCan=@iscan");
            htSyn.Add("iscan", isCan);
            objRecordCount = db.ExecuteScalar(strSql.ToString(),htSyn);
            if (objRecordCount != null)
            {
                i = Convert.ToInt32(objRecordCount);
            }
            return i;
        }
        #endregion

        #endregion  

        #region "前台产品分页 "
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="classid"></param>
        /// <param name="type"> 
        /// 产品类型
        /// 0,普通分页(带屏蔽效果)
        /// 1,推荐
        /// 2,首页展示，
        /// 3,热门
        /// 4,最新</param>
        /// <param name="iscan"></param>
        /// <returns></returns>
        public IList<product> pagingForProduct(int pageIndex, int pageSize, int classid, int type,bool iscan)
        {
            DataTable dt=null;
            IList<product> list = new List<product>();
            switch (type)
            {
                case 1:
                    dt = db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and iscan=0 and isBast=1 ", "productid", false);
                    break;
                case 2:
                    dt = db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and iscan=0 and isIndex=1 ", "productid", false);
                    break;
                case 3:
                    // dt = db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pinfo,pClassId,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,[Views],pKey,ptitle,pDescription,pButtom", "product", "and iscan=0", "Views", true);
                    break;
                case 4:
                    dt = db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and iscan=0 and IsNew=1 ", "productid", false);
                    break;
                default:
                    if (classid > 0)
                        dt = db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and iscan=0 and  pClassId='" + classid + "'", "productid", false);
                    else
                        dt = db.GetMPageDataTable(pageIndex, pageSize, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and iscan=0", "productid", false);
                    break;

            }
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    product product = new product();
                    product.productId = Convert.ToInt32(dr["productId"]);
                    product.productName = dr["productName"].ToString();
                    product.Price = Convert.ToDecimal(dr["Price"]);
                    product.pInfo = dr["pInfo"].ToString();
                    product.pClassId = Convert.ToInt32(dr["pClassId"]);
                    product.pKey = dr["pKey"].ToString();
                    product.pTitle = dr["ptitle"].ToString();
                    product.pDescription = dr["pDescription"].ToString();
                    product.pButtom = dr["pButtom"].ToString();
                    product.isCan = Convert.ToBoolean(dr["isCan"]);
                    product.isBast = Convert.ToBoolean(dr["isBast"]);
                    product.IsNew = Convert.ToBoolean(dr["IsNew"]);
                    product.isIndex = Convert.ToBoolean(dr["isIndex"]);
                    product.pDate = Convert.ToDateTime(dr["pDate"]);
                    product.eDate = Convert.ToDateTime(dr["eDate"]);
                    product.pSourse = dr["pSourse"].ToString();
                    product.ptag = dr["ptag"].ToString();
                    product.pSort = Convert.ToInt32(dr["pSort"]);
                    product.Views = Convert.ToInt32(dr["Views"]);
                    product.Productmodel = dr["productModel"].ToString();
                    product.Relationmark =Convert.ToInt32(dr["Relationmark"]);
                    list.Add(product);
                }
            }
            return list;
            
        }
         #endregion

        #region 获的前台产品总数
        /// <summary>
        /// 获的前台产品总数
         /// </summary>
         /// <param name="classid"></param>
         /// <param name="ptype">
         ///   产品类型
         /// 0,普通分页(带屏蔽效果)
         /// 1,推荐
         /// 2,首页展示，
         /// 3,热门
        /// 4,最新
        /// </param>
      /// <param name="iscan"></param>
      /// <returns></returns>
        public int getRecordCount(int classid,int ptype,bool iscan)
        {
            object objRecordCount = null;
            int i = 0;
            switch (ptype)
            {
                case 1:
                    objRecordCount = db.ExecuteScalar("select count(1) from product where  iscan=0 and isBast=1");
                    break;
                case 2:
                    objRecordCount = db.ExecuteScalar("select count(1) from product where  iscan=0 and isIndex=1");
                    break;
                case 3:
                    objRecordCount = db.ExecuteScalar("select count(1) from product where  iscan=0 order by views desc");
                    break;
                case 4:
                    objRecordCount = db.ExecuteScalar("select count(1) from product where  iscan=0 and IsNew=1");
                    break;
                default:
                    if (classid > 0)
                    {
                        objRecordCount = db.ExecuteScalar("select count(1) from product where  iscan=0 and pclassid=" + classid);
                    
                    }
                    else
                    {
                        objRecordCount = db.ExecuteScalar("select count(1) from product where  iscan=0 ");
                    }
                    break;

            }
            if (objRecordCount != null)
            {
                i = Convert.ToInt32(objRecordCount);
            }
            return i;
        }
        #endregion

        #region 随机数
        public DataTable getRandomProduct(int num)
        {
            strSql.Remove(0, strSql.Length);
            //查询出所有的产品ID号
            DataTable da = db.GetDataTable("SELECT productid from product where isCan=0 ");
            int pcount =da.Rows.Count;
            string[] pIdList = new string[pcount];//用一个数组存放所有的ID号
            string productids = "";//随机出来的ID
            if (pcount > num)//入参小于总数
            {
               
                Random rd = new Random();
                //用一个数组存放所有的索引号
                ArrayList temp = new ArrayList(num);
                //存入所有的ID
                for (int i = 0; i < pcount; i++)
                {
                    pIdList[i] = da.Rows[i]["productid"].ToString();
                }
                if (pIdList.Length > 0)//存入的数目大于0
                {
                    for (int j = 0; j < num; j++)
                    {
                        int index = rd.Next(0, pIdList.Length);//0到所有存入的ID中随机生成索引
                        if (temp.IndexOf(index) ==-1 )
                        {
                            temp.Add(index);
                        }
                        else
                        {
                            j = j - 1;
                            continue;
                        }
                    }
                    if (temp.Count > 0)
                    {
                        for (int i = 0; i < temp.Count; i++)
                        {
                            productids += pIdList[(int)temp[i]] + ",";
                        }
                        productids = productids.Substring(0, productids.Length - 1);
                    }
                }
            }
            else
            {
                if (pcount > 0)
                {
                    for (int i = 0; i < pcount; i++)
                    {
                        productids += da.Rows[i]["productid"].ToString() + ",";
                    }
                    productids = productids.Substring(0, productids.Length - 1);
                }
            }
            if (productids != "")
            {
                strSql.Append("select  productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark from product where isCan=0 and productid in ( ");
                strSql.Append(productids);
                strSql.Append(" )");
                strSql.Append(" order by pSort");
                
            }
            return db.GetDataTable(strSql.ToString());
            
        }
        #endregion

        #region 产品类型
        /// <summary>
      /// 产品类型
      /// 1,推荐
      /// 2,首页展示，
      /// 3,热门
      /// 4,最新
      /// </summary>
      /// <param name="num">数量</param>
        /// <param name="type">1,推荐2,首页展示，3,热门,4最新</param>
      /// <returns></returns>
        public DataTable getTypeProduct(int num,int type)
        {

            strSql.Remove(0, strSql.Length);
            strSql.AppendFormat("select top {0} productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark from product where isCan=0", num);
           if (type == 1)
           {
               strSql.Append(" and isBast=1");
           }
           else if (type == 2)
           {
               strSql.Append(" and isIndex=1");
           }
           else if (type == 3)
           {
               strSql.Append(" order by Views desc");
           }
           else if (type == 4)
           {
               strSql.Append(" and isBast=1");
           }
           else
           {
           }
           strSql.Append(" order by pSort desc");
           //htSyn.Add("@num", "top "+num);
           return db.GetDataTable(strSql.ToString());
       }
        #endregion
        
        #region 获取指定靠前的产品---郑同飞
        /// <summary>
       /// 获取指定靠前的产品---郑同飞
        /// </summary>
        /// <param name="productCount">产品数量</param>
        /// <param name="classid">产品类别</param>
        /// <returns></returns>
        public DataTable getProductBefor(int productCount,int classid) 
        {
            return db.GetMPageDataTable(1, productCount, "productId,productName,Price,pInfo,pClassId,pTitle,pKey,pDescription,pButtom,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,Views,productModel,Relationmark", "product", "and pClassId='" + classid + "' and iscan=0 ", "pSort", false);
        }
        #endregion

        #region 得到最新的产品---郑同飞
        /// <summary>
        /// 得到最新的产品---郑同飞
        /// </summary>
        /// <param name="productCount">获取数量</param>
        /// <returns></returns>
        public DataTable getNewProduct()
        {
            return db.GetDataTable("select productId,productName,Price,pinfo,pClassId,isCan,isBast,IsNew,isIndex,pDate,eDate,pSourse,ptag,pSort,[Views],pKey,ptitle,pDescription,pButtom,productModel,Relationmark from product where 1 > 0 and iscan=0 and IsNew=1  order by psort asc");
        }       
        #endregion



    }
}
